package com.thalesgroup.preferenceFramework.models.LCPnet.impl;

import junit.framework.TestCase;
import net.sourceforge.jFuzzyLogic.FIS;
import net.sourceforge.jFuzzyLogic.rule.FuzzyRuleSet;

import org.eclipse.emf.common.util.EList;
import org.junit.Before;
import org.junit.Test;

import com.thalesgroup.preferenceFramework.models.Node;
import com.thalesgroup.preferenceFramework.models.LCPnet.LCPnet;
import com.thalesgroup.preferenceFramework.models.LCPnet.LNode;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetEvaluationException;

public class LNodeImplTests extends TestCase {
	private LCPnet net;
	
	@Before
	public void setUp() throws Exception {
		net = LCPnetImpl.load("tests/com/thalesgroup/preferenceFramework/models/LCPnet/impl/lcpnet_test.lcpnet");
		assertNotNull(net);
	}

	@Test
	public void testGetFuzzyInferenceSystem() throws LCPnetEvaluationException {
		EList<Node> nodes = net.getNodes();
		for (Node node : nodes) {
			if(node.getName().equals("R")){
				LNode lnode = (LNode) node;
				
				FIS nodeFIS = lnode.getFuzzyInferenceSystem();
				assertNotNull(nodeFIS);
				
				FuzzyRuleSet fuzzyRuleSet = nodeFIS.getFuzzyRuleSet();
				assertNotNull(fuzzyRuleSet);
				
				// Set inputs
				fuzzyRuleSet.setVariable("B", 1);
				fuzzyRuleSet.setVariable("R", 0.6);
				// Evaluate fuzzy set
				fuzzyRuleSet.evaluate();
				
				// Show a chart for each variable in this ruleSet 
				//fuzzyRuleSet.chart();
				
				// Show output variable's chart 
				//fuzzyRuleSet.getVariable("Utility").chartDefuzzifier(true);
				
				// Print ruleSet
				//System.out.println(fuzzyRuleSet);
				
				assertEquals(fuzzyRuleSet.getVariable("Utility").getLatestDefuzzifiedValue(), 0.5576890210924856);
				return;
			}
		}
		fail("Node R not found in test file");
	}

}
